package com.ZengQi.event.dao.impl;

import com.ZengQi.event.dao.UserDao;
import com.ZengQi.event.entity.User;
import com.ZengQi.event.util.SearchUtils;
import com.ZengQi.event.util.UpdateUtils;

import java.util.List;

public class UserDaoImpl implements UserDao {
    @Override
    public boolean insertUser(User user) {
        String sql="insert into user values(null,?,?,?,?,?)";
        boolean update = UpdateUtils.update(sql, user.getLoginPnum(), user.getLoginPwd(), user.getUserName(), user.getUserSex(), user.getUserBirthday());

        return update;
    }

    @Override
    public User getUser(User conditionUser) {
        String sql="select user_id userId,login_pnum loginPnum,login_pwd loginPwd,user_name userName,user_sex userSex,user_birthday userBirthday from user where login_pnum=?";
        List<User> users = SearchUtils.searchForList(User.class, sql, conditionUser.getLoginPnum());
       if(users.size()!=0){
           return users.get(0);
       }else{
           return null;
       }

    }

    @Override
    public boolean updateUserByUserName(User user, String oldUserName) {
        //思路：sql语句修改所有信息，实际要不要修改看调用时的user对象的操作，若没有修改某词条则再次赋给他相同的值即可
        String sql="update user set user_name=?,user_sex=?,user_birthday=? where user_name=?";
        boolean update = UpdateUtils.update(sql, user.getUserName(), user.getUserSex(), user.getUserBirthday(),oldUserName);


        return update;
    }

    @Override
    public boolean getIsExistByUserName(User conditionUser) {
        String sql="select user_id userId,login_pnum loginPnum,login_pwd loginPwd,user_name userName,user_sex userSex,user_birthday userBirthday from user where user_name=?";
        List<User> users = SearchUtils.searchForList(User.class, sql, conditionUser.getUserName());
        if(users.size()==0){
            return false;
        }
        return true;

    }

    @Override
    public User getUserByUserName(User conditionUser) {
        String sql="select user_id userId,login_pnum loginPnum,login_pwd loginPwd,user_name userName,user_sex userSex,user_birthday userBirthday from user where user_name=?";
        List<User> users = SearchUtils.searchForList(User.class, sql, conditionUser.getUserName());
        return users.get(0);
    }
}
